﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

/**
 * 创建时间：2014/11/13 14:12:35
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace CUWo.Common.Converter
{
    /// <summary>
    /// 显示指定长度字符
    /// <para>一个中文字符算两个长度</para>
    /// </summary>
    public class ComplexStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return string.Empty;

            string s = value.ToString();

            int leng;
            if (parameter != null)
            {
                ResourceDictionary rd = parameter as ResourceDictionary;
                if (rd != null && rd.Count > 0)
                {
                    string showApostroph = string.Empty;
                    if (rd.Contains("ShowApostrophe"))
                    {
                        showApostroph = rd["ShowApostrophe"].ToString();
                    }
                    if (rd.Contains("ShowLength"))
                    {
                        if (int.TryParse(rd["ShowLength"].ToString(), out leng))
                        {
                            s = new Helper().GetString(s, leng);
                            s += showApostroph;
                        }
                    }
                }
            }
            return s;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return string.Empty;
        }
    }
}
